home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 120_01.zip / SCOTT.C < prev    next >
Text File  |  1993-06-01  |  4KB  |  175 lines

  1. /* HEADER: CUG120.23;
  2.    TITLE: SCOTT;
  3.    DATE: 10/29/1981;
  4.    DESCRIPTION: "Scott Layson's personal miscelaneous function library.";
  5.    KEYWORDS: library functions,stdlib?.c;
  6.    SYSTEM: CP/M;
  7.    FILENAME: SCOTT.C;
  8.    CRC: 094B;
  9.    AUTHORS: Scott Layson;
  10.    COMPILERS: BDS C;
  11. */
  12. /* SCOTT.C    Scott Layson's personal miscellaneous-function
  13.             library
  14.  
  15.     This code is in the public domain.
  16.     Created from functions in STDLIB?.C 81.10.29 Gyro
  17.  
  18. */
  19.  
  20.  
  21. #include "bdscio.h"
  22.  
  23.  
  24.     /* This deletes elements in the array <ary>, which consists of
  25.         <len> elements of <eltsize> bytes; elements from <dello>
  26.         through <delhi> - 1 are deleted. */
  27. arydel (ary, len, eltsize, dello, delhi)
  28.     char *ary;
  29.     int len, eltsize, dello, delhi;
  30. {
  31.     movmem (ary + delhi * eltsize,
  32.            ary + dello * eltsize,
  33.            (len - delhi) * eltsize);
  34.     }
  35.  
  36.  
  37. match (s1, s2)                /* case-independent null-term compare */
  38.     char *s1, *s2;
  39. {
  40.     do if (toupper (*s1) != toupper (*s2)) return (FALSE);
  41.         while ((s1++, *s2++));
  42.     return (TRUE);
  43.     }
  44.  
  45.  
  46. upcase (str)            /* convert a string to upper case */
  47.     char *str;
  48. {
  49.     for (;*str; ++str) *str = toupper (*str);
  50.     }
  51.  
  52.  
  53. /* The version of this in STDLIB2.C is bugous!!! */
  54. int
  55. fscanf(iobuf,format)
  56. char *format;
  57. struct _buf *iobuf;
  58. {
  59.     char text[MAXLINE];
  60.     if (!fgets(text,iobuf)) return /* 0 NO!!! */ -1;
  61.     return _scn(text,&format);
  62. }
  63.  
  64.  
  65. /* This is the standard one, except: if the character after "%s" in the format
  66. string is a '%', the %s breaks on any whitespace.  */
  67. int
  68. _scn(line,fmt)
  69. char *line, **fmt;
  70. {
  71.     char sf, c, base, n, *sptr, *format, matchchar;
  72.     int sign, val, **args;
  73.  
  74.     format = *fmt++;    /* fmt first points to the format string */
  75.     args = fmt;        /* now it points to the arg list */
  76.  
  77.     n = 0;
  78.     while (c = *format++) {
  79.        _igs (&line);
  80.        if (!*line) return n;    /* if end of input string, return */
  81.        if (isspace(c)) continue;    /* skip white space in format string */
  82.        if (c != '%') {        /* if not %, must match text */
  83.         if (c != _igs(&line)) return n;
  84.         else line++;
  85.         }
  86.        else {            /* process conversion */
  87.         sign = 1;
  88.         base = 10;
  89.         sf = 0;
  90.         if ((c = *format++) == '*') {
  91.             sf++;        /* if "*" given, supress assignment */
  92.             c = *format++;
  93.          }
  94.         switch (toupper(c)) {
  95.            case 'X': base = 16;
  96.                  goto doval;
  97.  
  98.            case 'O': base = 8;
  99.                  goto doval;
  100.  
  101.            case 'D': if (_igs(&line) == '-') {
  102.                 sign = -1;
  103.                 line++;
  104.                   }
  105.  
  106.        doval:  case 'U': val = 0;
  107.                  if (_bc(_igs(&line),base) == ERROR)
  108.                 return n;
  109.                  while ((c = _bc(*line++,base)) != 255)
  110.                 val = val * base + c;
  111.                  line--;
  112.                  break;
  113.  
  114.            case 'S': _igs(&line);
  115.                  sptr = *args;
  116.                 if (*format != '%') matchchar = *format++;
  117.                 else matchchar = '\0';
  118.                  while (c = *line++)   {
  119.                     if (matchchar ? c == matchchar : isspace (c)) break;
  120.                     if (!sf) *sptr++ = c;
  121.                   }                
  122.                  if (!sf) {
  123.                 n++;
  124.                 *sptr = '\0';
  125.                 args++;
  126.                   }
  127.                  continue;
  128.  
  129.            case 'C': if (!sf) {
  130.                 poke(*args++, *line);
  131.                 n++;
  132.                  }
  133.                  line++;
  134.                  continue;
  135.  
  136.            default:  return n;
  137.          }
  138.         if (!sf) {
  139.             **args++ = val * sign;
  140.             n++;
  141.          }
  142.     }}
  143.     return n;
  144. }
  145.  
  146.  
  147. /* This one doesn't leave a trailing newline on the end.  It also doesn't
  148.    care about CRs at all. */
  149. char *
  150. fgets(s,iobuf)
  151.     char *s;
  152.     struct _buf *iobuf;
  153. {
  154.     int count, c;
  155.     char *cptr;
  156.     
  157.     count = (MAXLINE - 1);
  158.     cptr = s;
  159.     if ((c = getc(iobuf)) == CPMEOF  ||  c == EOF) return NULL;
  160.     do {
  161.         if (c == '\r') continue;
  162.         if (c == '\n') break;
  163.         *cptr++ = c;
  164.         }
  165.         while (--count  &&  (c = getc(iobuf)) != EOF  &&  c != CPMEOF);
  166.     if (c == CPMEOF) ungetc(c,iobuf);    /* push back control-Z */
  167.     *cptr = '\0';
  168.     return s;
  169. }
  170.  
  171.  
  172.  
  173. /* End of SCOTT.C */
  174. n = 0;
  175.     while (c = *form